﻿using Customer.API.Applcation.Command.ClientCommand;
using Customer.Domain;
using Customer.Infrstructre.ClientManager;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.ClientCommandHandler
{
    /// <summary>
    /// 客户添加
    /// </summary>
    public class ClientCreateCommandHandler:IRequestHandler<ClientCreateCommand,int>
    {
        private readonly IClientRepository _clientRep;
        public ClientCreateCommandHandler(IClientRepository _clientRep)
        {
            this._clientRep = _clientRep;
        }

        public async Task<int> Handle(ClientCreateCommand request, CancellationToken cancellationToken)
        {
            foreach (var item in request.CusContactId)
            {
                var list = new Client
                {
                    ClientCode = request.ClientCode,
                    ClientName = request.ClientName,
                    Cfax = request.Cfax,
                    Landline = request.Landline,
                    Mailbox = request.Mailbox,
                    Cremark = request.Cremark,
                    CusContactId = item,
                    Office_Id = request.Office_Id,
                    User_Id = request.User_Id,
                    AddName = request.AddName,
                    AddTime = request.AddTime
                };
                await _clientRep.AddAsync(list);
            }
            return 1;
        }
    }
}
